package edu.jsnu.operate.controller.ticket;

import beans.PageQuery;
import com.github.pagehelper.PageInfo;
import dto.resDto.JsonData;
import dto.resDto.RespData;
import edu.jsnu.operate.controller.ticket.common.AbstractController;
import edu.jsnu.service.ticket.user.domain.User;
import edu.jsnu.service.ticket.user.dto.UserDto;
import edu.jsnu.service.ticket.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("ticket/user")
public class UserController extends AbstractController {

    @Autowired
    private UserService userService;

    @RequestMapping("user.page")
    public String userPage() {
        return "ticket/user/user";
    }

    @RequestMapping("save.page")
    public String userEditPage(Model model, @RequestParam(value = "id") Integer id) {
        User user = userService.getUserInfoById(id);
        model.addAttribute("user", user);
        return "ticket/user/userEdit";
    }

    @RequestMapping(value = "user.json", method = RequestMethod.GET)
    @ResponseBody
    public RespData userData(PageQuery pageQuery) {
        try {
            List<UserDto> userList = userService.getAllUser(pageQuery);
            PageInfo<UserDto> pageInfo = new PageInfo<>(userList);
            return RespData.success(pageInfo.getList(), pageInfo.getTotal());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return RespData.fail("查询用户数据失败！");
    }

    @RequestMapping(value = "save.json", method = RequestMethod.POST)
    @ResponseBody
    public JsonData saveUserInfo(User user) {
        try {
            return userService.saveUser(user) > 0 ? JsonData.success("更新用户信息成功") : JsonData.fail("更新用户信息失败，请稍后再试");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonData.fail("更新用户信息失败，请联系管理员");
    }

    @RequestMapping(value = "del.json", method = RequestMethod.POST)
    @ResponseBody
    public JsonData delUser(Integer id) {
        try {
            return userService.delUser(id) > 0 ? JsonData.success("删除用户成功") : JsonData.fail("删除用户失败，请稍后再试");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonData.fail("删除用户失败，请联系管理员");
    }

    @RequestMapping(value = "batchDel.json", method = RequestMethod.POST)
    @ResponseBody
    public JsonData batchDel(Integer[] ids) {
        try {
            return userService.batchDel(ids) > 0 ? JsonData.success("批量删除用户成功") : JsonData.fail("批量删除用户失败，请稍后再试");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonData.fail("批量删除用户失败，请联系管理员");
    }
}
